package com.yy.yyui.table
{
	import com.yy.yyui.IActionList;
	import com.yy.yyui.scroll.ScrollPanel;
	import com.util.tools.GT;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	

	public class Table extends Sprite implements IActionList
	{
		private var _actions:Object;
		private var _datagridPanel:ScrollPanel;	
		private var _datagrid:DataGrid;
		private var _tableHeaderPanel:Sprite;
		private var _cellHeaders:Array;
		private var _showTitle:Boolean; 	
		public static const TABEL_HEADER_HEIGHT:Number = 18;
		
		private var w:Number,h:Number;
		
		private var _tween:Boolean;
		
		private var headRightMargin:DisplayObject;
		
		public function Table(w:Number,h:Number,datagrid:DataGrid,showTitle:Boolean = true)
		{
			//TODO: implement function
			super();
			
			this.w = w;
			this.h = h;
			this._showTitle = showTitle;
			
			
			var sp:Sprite;
			var dobj:DisplayObject;
			
			this._datagrid = datagrid;
			this.datagrid.x = 0;
			this.datagrid.y = 0;
			
			//init panels
			sp = new Sprite();
			dobj = this.addChild(sp);
			//dobj.scrollRect = new Rectangle(0,0,this.width,Table.TABEL_HEADER_HEIGHT);
			this._tableHeaderPanel = sp;
			
			
			var srp:ScrollPanel = new ScrollPanel(this.width,this.height-TABEL_HEADER_HEIGHT);
			dobj = this.addChild(srp);
			dobj.y = TABEL_HEADER_HEIGHT;
			this._datagridPanel = srp;
			
			
			this.datagridPanel.addItem(this.datagrid);
			
			this._cellHeaders = new Array();
			
			//adjust 
			this.tableHeaderPanel.scrollRect = new Rectangle(0,0,this.datagridPanel.contentScrollPanel.width,Table.TABEL_HEADER_HEIGHT);
			
			//shape
			dobj = this.addChild(new Shape());
			dobj.x = this.tableHeaderPanel.scrollRect.right;
//			GT.drawAlphaGradientBackground(dobj,1,TABEL_HEADER_HEIGHT);
			dobj.alpha = 0;
			this.headRightMargin = dobj;
			
			
			//set actions
			this.datagrid.addEventListener(DataGrid.DATAGRID_UPDATE,updateHeader);
			this._actions = {};
			this.tween = false;
			this.datagrid.update();
			
			this.addEventListener(Event.REMOVED_FROM_STAGE,destroy);
		}
		
		public function destroy(...rest):void{
			this.removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
			GT.destroyActions(this.actions);	
			this.datagrid.removeEventListener(DataGrid.DATAGRID_UPDATE,updateHeader);	
		}
		
		
		private function updateHeader(e:Event):void{
			update();
		}
		
		public function update():void{
			var i:int,l:int;
			var header:Header;
			var locations:Array = this.datagrid.columnLocations;
			var cellWidth:Number;
			var headerWidth:Number;
			var showHeadRightMargin:Boolean = false;
			var rect:Rectangle; 
			for(i=0,l=locations.length;i<l;i++){
				var label:String = this.datagrid.getColumnLabel(i);
				rect = locations[i] as Rectangle;
				if(this.cellHeaders[i]==null){
					header = new Header("",1);
					this.cellHeaders[i] = header;
					this.tableHeaderPanel.addChild(header);
					header.alpha = 0;
				}else{
					header = this.cellHeaders[i] as Header;
				}
				header.label = label;
				if(tween){
						header.width = rect.width;
						header.x = rect.x;
						header.alpha = 1;
				}else{
					header.alpha = 1;
					header.width = rect.width;
					header.x = rect.x;
					header.visible = this._showTitle;
				}
				if(rect.right>=this.tableHeaderPanel.scrollRect.right){
					showHeadRightMargin = true;
				}
			}
			if(showHeadRightMargin){
				this.headRightMargin.x = this.tableHeaderPanel.scrollRect.right;
				//this.headRightMargin.x = rect.right+this.datagrid.gap;
				this.headRightMargin.alpha = 1;
				this.headRightMargin.width = this.width-this.headRightMargin.x;
			}else{
				this.headRightMargin.alpha = 0;
			}
			
					
		} 
		
		public function get actions():Object{return this._actions;}
		public function get tableHeaderPanel():Sprite{return this._tableHeaderPanel;}
		public function get cellHeaders():Array{return this._cellHeaders;}
		public function get datagridPanel():ScrollPanel{return this._datagridPanel;}
		public function get datagrid():DataGrid{return this._datagrid;}
		
		public function get tween():Boolean{return this._tween;}
		public function set tween(value:Boolean):void{
			this._tween = value;
			this.datagrid.tween = value;
		}		
		
		override public function get width():Number{return w;}
		override public function get height():Number{return h;}
		
		override public function set width(newWidth : Number) : void {
			var diff:Number = newWidth - w;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;			
			w = newWidth;
		}
		
		override public function set height(newHeight : Number) : void {
			var diff:Number = newHeight - h;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;			
			h = newHeight;			
		}			
		
	}
}
	import com.maqie.GlobalConfig;
	import com.maqie.view.UM;
	import com.yy.yyui.table.Table;
	import com.util.tools.GT;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	
class Header extends Sprite{
	
	private var w:Number,h:Number;
	private var _label:String;
	private var tl:DisplayObject;
	private var _bg:DisplayObject;
	public function Header(label:String,width:Number){
		super();
		var dobj:DisplayObject;
		this.h = Table.TABEL_HEADER_HEIGHT;
		this.w = width;
		this._bg = UM.createEmptyDisplayObject(0,0);
		this._bg.width = this.w;
		this._bg.height = this.h+2;
		dobj = this.addChild(_bg);
		dobj.y += 20;
		this._label = label;
		tl = UM.createLabel(label,false,false,0x79c2be,GlobalConfig.LABEL_SIZE,null,false);
		this.addChild(tl);
		GT.centerScrollChild(this,tl);
	}
	public function set label(value:String):void{
		this._label = value;
		tl = UM.setLabelText(value,this.tl);
		tl.width = this.width;
		bg.width = this.width;
		GT.centerScrollChild(this,tl);
		bg.x = this.scrollRect.x;
		bg.y = this.scrollRect.y;
	}
	public function get label():String{return this._label;}
	private function get bg():DisplayObject{return this._bg;}

	override public function get width():Number{return w;}
	override public function get height():Number{return h;}
	
	override public function set width(newWidth : Number) : void {
		var diff:Number = newWidth - w;
		var oldW:Number,oldH:Number;
		var i:uint,l:uint;			
		w = newWidth;
		bg.width = this.width;
		tl.width = this.width;
		GT.centerScrollChild(this,tl);
		bg.x = this.scrollRect.x;
	}
	
	override public function set height(newHeight : Number) : void {
		var diff:Number = newHeight - h;
		var oldW:Number,oldH:Number;
		var i:uint,l:uint;			
		h = newHeight;			
	}
}